package com.yima.baseproj.module.tmpl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yima.baseproj.config.exception.type.ApiException;
import com.yima.baseproj.module.config.BaseController;
import com.yima.baseproj.module.config.utils.PageUtil;
import com.yima.baseproj.module.config.utils.PageVo;
import com.yima.baseproj.module.tmpl.model.AppTmplAddParamModel;
import com.yima.baseproj.module.tmpl.model.AppTmplEditParamModel;
import com.yima.baseproj.module.tmpl.model.AppTmplSearchModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author 一码
 * @createDate 2023/4/1 09:00
 * @contact 1424529270@qq.com
 */
@Api(tags = "模板接口",value = "为写接口时做参考")
@RestController
@RequestMapping("/api/app/tmpl")
public class AppTmplController extends BaseController {

    @Resource
    private AppTmplService appTmplService;

    @ApiOperation("查询分页")
    @GetMapping(value = "/findAllByPage")
    public Object findAllByPage(PageVo page, AppTmplSearchModel searchVo) {
        Page<Map> pageData = appTmplService.findByPage(PageUtil.initMpPage(page),searchVo);
        return pageData;
    }

    @ApiOperation("查询列表")
    @GetMapping(value = "/findAllByList")
    public Object findAllByList(AppTmplSearchModel searchVo) {
        List<Map> list = appTmplService.findByList(searchVo);
        return list;
    }

    @ApiOperation("查询详情")
    @GetMapping(value = "/findById/{id}")
    public Map findById(@PathVariable String id) {
        AppTmplEntity appTmplEntity = appTmplService.getById(id);
        if(appTmplEntity == null){
            throw ApiException.getInstance("数据不存在，请刷新重试");
        }

        Map result = JSON.parseObject(JSON.toJSONString(appTmplEntity),Map.class);

        return result;
    }

    @ApiOperation("添加")
    @PostMapping(value = "/add")
    public void add(@Validated @RequestBody AppTmplAddParamModel paramModel) {
        AppTmplEntity appTmplEntity = new AppTmplEntity();
        appTmplEntity.setData(paramModel);
        appTmplService.save(appTmplEntity);
    }

    @ApiOperation("修改")
    @PostMapping(value = "/edit")
    public void edit(@Validated @RequestBody AppTmplEditParamModel paramModel) {
        //校验参数
        AppTmplEntity appTmplEntity = appTmplService.getById(paramModel.getId());
        if(appTmplEntity == null){
            throw  new ApiException("数据不存在，请刷新重试");
        }
        appTmplEntity.setData(paramModel);
        appTmplService.updateById(appTmplEntity);
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/delByIds/{ids}")
    public void delAllByIds(@PathVariable String[] ids) {
        appTmplService.dels(ids);
    }
}